package com.filetransfer.config;
import com.filetransfer.api.ApiHelper;
import com.filetransfer.api.generated.MessageControllerApi;
import com.filetransfer.api.generated.invoker.ApiException;
import com.filetransfer.api.generated.models.Message;
import com.filetransfer.utils.RetryExecutor;
import com.filetransfer.utils.StringFormat;
import de.exlll.configlib.Comment;
import de.exlll.configlib.Configuration;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Getter
@Setter
@Configuration
public final class UserThemeConfiguration {
    @Comment("The version of the configuration")
    private String version = "1.0.0";
    @Comment("The name of the selected theme")
    private String themeName = "DefaultTheme";
    @Comment("The address of gitcode")
    private String gitCode = "https://gitcode.com/file_transfer";
    @Comment("The address of the server")
    private String serverAddress = "http://localhost:8081";
    @Comment("The directory where files will be downloaded")
    private String downloadDirectory = getUserDownloadDirectory();
    @Comment("The last time the configuration was updated")
    private String lastUpdated = "2024-12-24 12:00:00";
    @Comment("The upload history")
    private List<UploadHistory> uploadHistory = new ArrayList<>();
    public UserThemeConfiguration() {
    }
    public String getUserDownloadDirectory() {
        String userHome = System.getProperty("user.home");
        return userHome + File.separator + "Downloads";
    }
    @Data
    @Configuration // 添加此注解
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UploadHistory {
        @Comment("The name of the uploaded file")
        private String fileName;
        @Comment("The path of the uploaded file")
        private String filePath;
        @Comment("The date and time the file was uploaded")
        private String uploadDate;
        @Comment("The share code of the uploaded file")
        private String shareCode;
        @Comment("The user's custom message for the upload")
        private String message;
        @Comment("token")
        private String editToken = "";
        public void addMessage(String userMessage1) {
            this.message += "\n" + userMessage1;
        }
    }
    public void updateMessagesFromServer() {
        MessageControllerApi api = new MessageControllerApi(ApiHelper.newApiClient());
        RetryExecutor<Void> retryExecutor = new RetryExecutor<>(3, 2000, 4);
        for (UploadHistory history : uploadHistory) {
            retryExecutor.submit(() -> {
                List<Message> userMessages = null;
                try {
                    userMessages = api.getMessages(history.getShareCode());
                } catch (ApiException e) {
                    throw new RuntimeException(e);
                }
                String message = StringFormat.formatMessage(userMessages);
                history.setMessage(message);
                log.info("Message updated for file: " + history.getFileName());
                return null;
            }, result -> {
                if (result.isSuccess()) {
                    log.info("Successfully updated message for file: " + history.getFileName());
                } else {
                    log.error("Failed to update message for file: " + history.getFileName());
                }
            });
        }
        // 添加关闭钩子，确保资源释放
        Runtime.getRuntime().addShutdownHook(new Thread(retryExecutor::shutdown));
    }
}
